package pl.com.margielewski.servicetimetracker;

import java.text.SimpleDateFormat;

import pl.com.margielewski.sttdatabase.DatabaseManager;
import pl.com.margielewski.sttdatabase.sqltables.SQLActualState;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	private SQLiteDatabase db;

	private void displayNotImplementedMehtod() {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setCancelable(false);
		alertDialog
				.setMessage(this.getString(R.string.litNotImplementedMethod));
		alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
				this.getString(R.string.litOK),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();

					}
				});
		alertDialog.show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		DatabaseManager dbManager = new DatabaseManager(this);
		try {
			this.db = dbManager.getWritableDatabase();
		} catch (Exception ex) {
			Log.d("d", ex.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.addNewService:

			Intent inAddNewService = new Intent(this, AddNewService.class);
			this.startActivity(inAddNewService);

			/*
			 * SQLActualState acState = new SQLActualState();
			 * acState.setBooks(1); acState.setBrochures(0); acState.setDate(new
			 * Date()); acState.setUser(Tools.getEmail(this));
			 * acState.setHours(3); acState.setId(0); acState.setMagazines(6);
			 * acState.setReguralReaders(0); acState.setStudies(0);
			 * acState.setVisits(2);
			 * 
			 * acState.insert(this.db); this.displayNotImplementedMehtod();
			 */
			return true;
		case R.id.reportActualMonth:

			// this.showDialog(MODE_WORLD_READABLE);
			this.displayNotImplementedMehtod();
			return true;
		case R.id.showHistoryOfMonths:
			this.displayNotImplementedMehtod();
			return true;
		case R.id.showHistoryOfWorkouts:
			this.displayNotImplementedMehtod();
			return true;
		case R.id.exportDataOnServer:
			this.displayNotImplementedMehtod();
			return true;
		case R.id.showAboutProgram:
			this.displayNotImplementedMehtod();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		SQLActualState actualState = new SQLActualState(1);
		if (actualState.getDataFromDB(this.db)) {

			TextView tv = (TextView) findViewById(R.id.tvUserName);
			tv.setText(String.valueOf(actualState.getUser()));

			tv = (TextView) findViewById(R.id.tvBooks);
			tv.setText(String.valueOf(actualState.getBooks()));

			tv = (TextView) findViewById(R.id.tvBrochure);
			tv.setText(String.valueOf(actualState.getBrochures()));

			tv = (TextView) findViewById(R.id.tvHours);
			tv.setText(String.valueOf(actualState.getHours()));

			tv = (TextView) findViewById(R.id.tvMagazines);
			tv.setText(String.valueOf(actualState.getMagazines()));

			tv = (TextView) findViewById(R.id.tvRegularReaders);
			tv.setText(String.valueOf(actualState.getReguralReaders()));

			tv = (TextView) findViewById(R.id.tvStudies);
			tv.setText(String.valueOf(actualState.getStudies()));

			tv = (TextView) findViewById(R.id.tvVisits);
			tv.setText(String.valueOf(actualState.getVisits()));

			tv = (TextView) findViewById(R.id.tvModyficationDate);
			tv.setText(this.getString(R.string.litModyficationDate)
					+ " "
					+ new SimpleDateFormat("dd-MM-yyyy").format(actualState
							.getDate()));
		}
	}
}